/*********************************************************************
 *
 *  elksip - a multi-platform Sip Phone Server
 *
 *  file: elk_sip_address.c  
 *
 *  Copyright 2011-2014 Elksip.
 *
 ********************************************************************/
#include "elk_sip_address.h"
#include "elk_msg.h"
#include "elk_port.h"
#include "sip_inc.h"
#include "elk_debug.h"

void elk_address_init(Elk_Address *from)
{
    memset(from, 0, sizeof(Elk_Address)); 
}
void elk_address_free(Elk_Address *from)
{
    if(from!=NULL)
    {
        ELK_FREE(from->display);
        ELK_FREE(from->name);
        ELK_FREE(from->domain);
        ELK_FREE(from->tag);
    }
}
void elk_address_cpy(const Elk_Address *src, Elk_Address *dest)
{
    dest->display = elk_strdup(src->display);
    dest->name = elk_strdup(src->name);
    dest->domain = elk_strdup(src->domain);
    dest->tag = elk_strdup(src->tag);
}
//To: Bob <sip:bob@biloxi.com>
//From: sip:+12125551212@server.phone2net.com;tag=887s  //todo
int elk_address_parse(Elk_Address *address, const char* value)
{
    const char *less_index = strchr(value, '<');
    if(less_index==NULL)
        return -1;
    //address->display = strndup(value, less_index-value-1);
    int display_len = less_index-value;
    if(display_len>0)
    {
        address->display = (char*)ELK_MALLOC( display_len+1 );
        snprintf(address->display, display_len+1, "%s", value);
        address->display[display_len] = '\0';
    }
    
    const char *colon_index = strchr(less_index, ':');
    if(colon_index==NULL)
    {   //To: Bob <bob@biloxi.com>
        return -1;
    }
    const char *art_index = strchr(colon_index, '@');
    if(art_index==NULL)
        return -1;
        
    int len = art_index-colon_index;
    char buf[64] = {0};
    strncpy(buf, colon_index+1, len-1);
    address->name = strdup(buf);
    const char *great_index = strchr(art_index, '>');
    if(great_index==NULL)
        return -2;
    len = great_index -art_index;
    address->domain = (char*) ELK_MALLOC(len);
    memset(address->domain, 0, len);
    strncpy(address->domain, art_index+1, len-1);
    //printf("zhang\n");
    //printf("zhang  :[%s]\n", to->domain);
    const char *equal_index = strchr(great_index, '=');
    if(equal_index==NULL)
        return 0;
    if(equal_index!=NULL)
    {
        equal_index++;
        address->tag = strdup(equal_index);
    }
    return 0;
}
void elk_from2str(const Elk_Address *from, Elk_string *str)
{
    if(from==NULL)
        return;
 
    char buf[128] = {0};
    if(from->display==NULL)
        snprintf(buf, sizeof(buf), "From: <sip:%s@%s>;tag=%s",
            from->name, from->domain, from->tag);
    else
        snprintf(buf, sizeof(buf), "From: %s <sip:%s@%s>;tag=%s",
            from->display, from->name, from->domain, from->tag);
    elk_str_append(str, buf);
    elk_str_append_CRLF(str);
}
void elk_to2str(const Elk_Address *to, Elk_string *str)
{
    char buf[128] = {0};
    int len=0;
    if(to==NULL) return;

    if(to->display!=NULL)
    {
        len = snprintf(buf, sizeof(buf), "To: %s ", to->display);
    }else
    {
        len = snprintf(buf, sizeof(buf), "To: ");
    }
    if(to->tag!=NULL)
    {
        snprintf(buf+len, sizeof(buf)-len, "<sip:%s@%s>;tag=%s",
            to->name, to->domain, to->tag);

    }else
    {
        snprintf(buf+len, sizeof(buf)-len, "<sip:%s@%s>",
            to->name, to->domain);
    }
    elk_str_append(str, buf);
    elk_str_append_CRLF(str);
}
void elk_address_print(Elk_Address *from)
{
    if(from ==NULL)
        return;
    if(from->display!=NULL)
        SIP_DEBUG_DEBUG("display=[%s]\n", from->display);
    if(from->name != NULL)
        SIP_DEBUG_DEBUG("name=[%s]\n", from->name);
    if(from->domain!=NULL)
        SIP_DEBUG_DEBUG("domain=[%s]\n", from->domain);
    if(from->tag!=NULL)
        SIP_DEBUG_DEBUG("tag=[%s]\n", from->tag);

}

